﻿using AutoMapper;
using MediatR;
using Provenance.Domain.Breed;
using Provenance.ErrorCount;
using Provenance.Infra.Interfaces.Breed;
using Provenance.Infra.Repositories.Breed;
using Provenance.Write.Api.Application.Command.Breed;

namespace Provenance.Write.Api.Application.CommandHandler.Breed
{
    public class InsertBreedHealthCommandHandler : IRequestHandler<InsertBreedHealthCommand, ApiResult>
    {
        private readonly IBreedHealthRepository _breedHealthRepository;
        private readonly ILogger<InsertBreedHealthCommandHandler> _logger;
        private IMapper _mapper;
        public InsertBreedHealthCommandHandler(IBreedHealthRepository breedHealthRepository, IMapper mapper, ILogger<InsertBreedHealthCommandHandler> logger)
        {
            _breedHealthRepository = breedHealthRepository;
            _mapper = mapper;
            _logger = logger;
        }
        public async Task<ApiResult> Handle(InsertBreedHealthCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var obj = _mapper.Map<BreedHealth>(request);
                await _breedHealthRepository.InsertEntity(obj);
                return ApiResult.Success();
            }
            catch (Exception ex)
            {
                _logger.LogError("Error in InsertBreedHealthCommandHandler: " + ex.Message);
                return ApiResult.Failure(ex.Message);
            }
        }
    }
}
